'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Class TlvDcInfo
    Inherits Tlv

    Public Sub New()
        MyBase.New(&HC)
    End Sub

    Private _DcInternalIpAddress As Long
    Public Property DcInternalIpAddress() As Long
        Get
            Return _DcInternalIpAddress
        End Get
        Set(ByVal value As Long)
            _DcInternalIpAddress = value
        End Set
    End Property

    Private _DcPort As Integer
    Public Property DcPort() As Integer
        Get
            Return _DcPort
        End Get
        Set(ByVal value As Integer)
            _DcPort = value
        End Set
    End Property

    Private _DCType As DcType
    Public Property DcByte() As DcType
        Get
            Return _DCType
        End Get
        Set(ByVal value As DcType)
            _DCType = value
        End Set
    End Property

    Private _DcProtocolVersion As Integer
    Public Property DcProtocolVersion() As Integer
        Get
            Return _DcProtocolVersion
        End Get
        Set(ByVal value As Integer)
            _DcProtocolVersion = value
        End Set
    End Property

    Private _DcAuthCookie As Long
    Public Property DcAuthCookie() As Long
        Get
            Return _DcAuthCookie
        End Get
        Set(ByVal value As Long)
            _DcAuthCookie = value
        End Set
    End Property

    Private _WebFrontPort As Long
    Public Property WebFrontPort() As Long
        Get
            Return _WebFrontPort
        End Get
        Set(ByVal value As Long)
            _WebFrontPort = value
        End Set
    End Property

    Private _LastInfoUpdate As DateTime
    Public Property LastInfoUpdate() As DateTime
        Get
            Return _LastInfoUpdate
        End Get
        Set(ByVal value As DateTime)
            _LastInfoUpdate = value
        End Set
    End Property

    Private _LastExtInfoUpdate As Date
    Public Property LastExtInfoUpdate() As Date
        Get
            Return _LastExtInfoUpdate
        End Get
        Set(ByVal value As Date)
            _LastExtInfoUpdate = value
        End Set
    End Property

    Private _LastExtStatusUpdate As Date
    Public Property LastExtStatusUpdate() As Date
        Get
            Return _LastExtStatusUpdate
        End Get
        Set(ByVal value As Date)
            _LastExtStatusUpdate = value
        End Set
    End Property

    Public Overrides Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte))
        MyBase.Deserialize(data)

        Dim index As Integer = Tlv.SizeFixPart

        Try
            
            _DcInternalIpAddress = ByteConverter.ToUInt32(data.GetRange(index, 4))
            index += 4

            _DcPort = CInt(ByteConverter.ToUInt32(data.GetRange(index, 4)))
            index += 4

            _DCType = DirectCast(data(index), DcType)
            index += 1

            _DcProtocolVersion = ByteConverter.ToUInt16(data.GetRange(index, 2))
            index += 2

            _DcAuthCookie = ByteConverter.ToUInt32(data.GetRange(index, 4))
            index += 4

            _WebFrontPort = ByteConverter.ToUInt32(data.GetRange(index, 4))
            index += 4

            'Client futures 
            index += 4

            _LastInfoUpdate = ByteConverter.ToDateTimeFromUInt32FileStamp(data.GetRange(index, 4))
            index += 4

            _LastExtInfoUpdate = ByteConverter.ToDateTimeFromUInt32FileStamp(data.GetRange(index, 4))
            index += 4

            _LastExtStatusUpdate = ByteConverter.ToDateTimeFromUInt32FileStamp(data.GetRange(index, 4))
            index += 4
        Catch ex As Exception
            If Debugger.IsAttached Then Debugger.Break()
        End Try
    End Sub

    Public Overrides Function Serialize() As System.Collections.Generic.List(Of Byte)
        Dim data As List(Of Byte) = MyBase.Serialize()

        data.AddRange(ByteConverter.GetBytes(CUInt(_DcInternalIpAddress)))
        data.AddRange(ByteConverter.GetBytes(CUInt(_DcPort)))
        data.Add(_DCType)
        data.AddRange(ByteConverter.GetBytes(CUInt(_DcProtocolVersion)))
        data.AddRange(ByteConverter.GetBytes(CUInt(_DcAuthCookie)))
        data.AddRange(ByteConverter.GetBytes(CUInt(_WebFrontPort)))
        data.AddRange(ByteConverter.GetBytes(CUInt(3)))
        data.AddRange(ByteConverter.GetBytesForUInt32Date(_LastInfoUpdate))
        data.AddRange(ByteConverter.GetBytesForUInt32Date(_LastExtInfoUpdate))
        data.AddRange(ByteConverter.GetBytesForUInt32Date(_LastExtStatusUpdate))

        Return data
    End Function

    '   xx xx xx xx   dword   DC internal ip address 
    'xx xx xx xx   dword   DC tcp port 
    'xx   byte   DC type 
    'xx xx   word   DC protocol version 
    'xx xx xx xx   dword   DC auth cookie 
    'xx xx xx xx   dword   Web front port 
    '00 00 00 03   dword   Client futures 
    'xx xx xx xx   dword   last info update time 
    'xx xx xx xx   dword   last ext info update time (i.e. icqphone status) 
    'xx xx xx xx   dword   last ext status update time (i.e. phonebook) 
    'xx xx   word   unknown 

    Public Overrides Function CalculateDataSize() As Integer
        Return 4 + 4 + 1 + 2 + 4 + 4 + 4 + 4 + 4 + 4 + 2
    End Function
End Class

Public Enum DcType As Byte
    DirectConnectionDisabledAuthRequired = &H0
    DirectConnectionThruFirewallOrHttpsProxy = &H1
    DirectConnectionThruSocks45ProxyServer = &H2
    NormalDirectConnectionWithoutProxyFirewall = &H4
    WebClientNoDirectConnection = &H6
End Enum